import requests
import re


class Spider:
    def start_request(self, url):
        response = requests.get(url)
        html = response.text
        src_list = re.findall('data-original="(.*?)"', html)
        alt_list = re.findall('alt="(.*?)"', html)
        for s, a in zip(src_list, alt_list):
            self.down_img(s, a)

    def down_img(self, s, a):
        try:
            r = requests.get(s)
            print("正在抓取: " + s[:20] + "...")
            f_name = a + ".jpg"
            with open(f_name, "wb") as f:
                f.write(r.content)
        except:
            print("请求错误")


if __name__ == '__main__':
    spider = Spider()
    url = "https://www.huya.com/g/xingxiu"
    spider.start_request(url)